<?php

namespace app\http\controller;

use tmcore\services\pay\AlipayService;

class Alipay extends HttpController
{
    /**
     * 支付宝支付实例
     * @var AlipayService
     */
    protected AlipayService $payService;

    /**
     * 初始化方法
     */
    public function initialize()
    {
        $this->payService = new AlipayService();
    }

    /**
     * 异步回调
     * @return \Psr\Http\Message\ResponseInterface|string
     */
    public function notify() {
        $notify = $this->payService->notify();
        if($notify) {
            if(isset($notify['trade_status']) && $notify['trade_status'] == 'TRADE_SUCCESS') {

            }
            return $this->payService->notifySuccess();
        } else {
            return '验签失败';
        }
    }

    /**
     * 同步回调
     * @return \Psr\Http\Message\ResponseInterface|string
     */
    public function return() {
        $notify = $this->payService->notify();
        if($notify) {
            if(isset($notify['trade_status']) && $notify['trade_status'] == 'TRADE_SUCCESS') {

            }
            return $this->payService->notifySuccess();
        } else {
            return '验签失败';
        }
    }

    /**
     * 获取回调参数
     * @return mixed
     */
    private function getNotifyData() {
        return input('post.');
    }
}